package www.yingdi365.com.activity;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.AppMicrofacies;
import www.yingdi365.com.util.ScreenUtils;

public class WeiXiangBenActivity extends ZjbBaseActivity implements View.OnClickListener {
    //    private EasyRecyclerView recyclerView;
//    private RecyclerArrayAdapter<AppMicrofaciesPaging.MicrofaciesBean.PictureListBean> adapter;
    private Dialog mDialog;
    private View viewShare;
    private View imageCancle;
    private boolean isStart = false;
    private AppMicrofacies microfaciesBean;
    private List<AppMicrofacies.PictureListBean> pictureList;
    private ListView listView;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case Constant.BROADCASTCODE.WeiXiangCeBaoCun:
                    microfaciesBean = (AppMicrofacies) intent.getSerializableExtra(Constant.INTENT_KEY.MicrofaciesBean);
                    textViewTitle.setText(microfaciesBean.getName());
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    };
    private TextView textViewTitle;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_xiang_ben);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        microfaciesBean = (AppMicrofacies) intent.getSerializableExtra(Constant.INTENT_KEY.MicrofaciesBean);
        pictureList = microfaciesBean.getPictureList();
    }

    @Override
    protected void findID() {
//        recyclerView = (EasyRecyclerView) findViewById(R.id.recyclerView);
        textViewTitle = (TextView) findViewById(R.id.textViewTitle);
        listView = (ListView) findViewById(R.id.listView);
    }

    @Override
    protected void initViews() {
        textViewTitle.setText(microfaciesBean.getName());
//        initRecycle();
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageBack).setOnClickListener(this);
        findViewById(R.id.imageShare).setOnClickListener(this);
        findViewById(R.id.imageBoFang).setOnClickListener(this);
        findViewById(R.id.imageBianJiWXB).setOnClickListener(this);
    }

    @Override
    protected void initData() {
//        adapter.clear();
//        adapter.addAll(pictureList);
    }

//    private void initRecycle() {
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//        recyclerView.setLayoutManager(layoutManager);
//        DividerDecoration itemDecoration = new DividerDecoration(Color.TRANSPARENT, (int) DpUtils.convertDpToPixel(5f, this), 0, 0);
//        recyclerView.addItemDecoration(itemDecoration);
//        int red = getResources().getColor(R.color.basic_color);
//        recyclerView.setRefreshingColor(red);
//        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter<AppMicrofaciesPaging.MicrofaciesBean.PictureListBean>(this) {
//            @Override
//            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
//                int layout = R.layout.item_weixiangben_yl;
//                return new WeiXiangBenYLViewHolder(parent, layout);
//            }
//        });
//        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
//            @Override
//            public void onErrorShow() {
//                adapter.resumeMore();
//            }
//
//            @Override
//            public void onErrorClick() {
//                adapter.resumeMore();
//            }
//        });
//        adapter.addHeader(new RecyclerArrayAdapter.ItemView() {
//            @Override
//            public View onCreateView(ViewGroup parent) {
//                View view = new View(WeiXiangBenActivity.this);
//                view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) DpUtils.convertDpToPixel(5f, WeiXiangBenActivity.this)));
//                return view;
//            }
//
//            @Override
//            public void onBindView(View headerView) {
//
//            }
//        });
//        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(int position) {
////                Intent intent= new Intent();
////                intent.setClass(getActivity(), ShangHuXQActivity.class);
////                startActivity(intent);
//            }
//        });
//    }

//    private OkObject getOkObject() {
//        LogUtil.LogShitou("WeiXiangBenActivity--getOkObject", "uuid  "+microfaciesBean.getUuid());
//        String url = Constant.HOST +Constant.Url.AppMicrofaciesService+"microfacies/get/"+microfaciesBean.getUuid();
//        HttpHeaders headers = new HttpHeaders();
//        headers.put(Constant.TOKEN, appLoginResult.getToken());
//        HashMap<String, String> params = new HashMap<>();
//        return new OkObject(headers, params, url);
//    }

    class MyAdapter extends BaseAdapter {
        class ViewHolder {
            public ImageView image;
        }

        @Override
        public int getCount() {
            return pictureList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(WeiXiangBenActivity.this).inflate(R.layout.item_weixiangben_yl, null);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ViewGroup.LayoutParams layoutParams = holder.image.getLayoutParams();
            layoutParams.width = ScreenUtils.getScreenWidth(WeiXiangBenActivity.this);
            layoutParams.height = (int) (ScreenUtils.getScreenWidth(WeiXiangBenActivity.this) * pictureList.get(position).getKuanGaoBi());
            holder.image.setLayoutParams(layoutParams);
            Glide.with(WeiXiangBenActivity.this)
                    .load(pictureList.get(position).getUrl() + Constant.TuPianYaSuo)
//                    .placeholder(R.mipmap.album)
                    .into(holder.image);
            return convertView;
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.imageBianJiWXB:
                intent.setClass(this, BianJiWXBActivity.class);
                intent.putExtra(Constant.INTENT_KEY.MicrofaciesBean, microfaciesBean);
                startActivity(intent);
                break;
            case R.id.imageBoFang:
                intent.setClass(this, WeiXiangBenBFActivity.class);
                intent.putExtra(Constant.INTENT_KEY.MicrofaciesBean, microfaciesBean);
                startActivity(intent);
                break;
            case R.id.imageShare:
                share();
                break;
            case R.id.imageBack:
                finish();
                break;
        }
    }

    private void share() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_fen_xiang, null);
        viewShare = inflate.findViewById(R.id.viewShare);
        imageCancle = inflate.findViewById(R.id.imageCancle);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.wang_shang_fang_da);
        animation.setFillAfter(true);
        if (animation != null) {
            viewShare.startAnimation(animation);
        }
        imageCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancle();
            }
        });
        mDialog = new Dialog(this, R.style.fullDialog);
        mDialog.setContentView(inflate);
        mDialog.setCancelable(false);
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    cancle();
                }
                return false;
            }
        });
        mDialog.show();
    }

    private void cancle() {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.wang_xia_suo_xiao);
        animation.setFillAfter(true);
        if (animation != null && !isStart) {
            viewShare.startAnimation(animation);
            isStart = true;
        }
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                viewShare.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mDialog.dismiss();
                        isStart = false;
                    }
                }, 0);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.BROADCASTCODE.WeiXiangCeBaoCun);
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}
